const ErrorResult = require('./handle.js')
// 异常处理中间件
const abnormal = async (ctx, next) => {
  try {
    await next()
  } catch (err) {
    if (err instanceof ErrorResult) {
      // 已知错误
      ctx.body = {
        msg: err.msg,
      }
      ctx.status = err.code
    } else {
      // 未知错误
      ctx.body = {
        msg: '服务器错误'
      }
      ctx.status = 502
    }
  }
}

module.exports = abnormal